package com.itrip.controller;

import com.itrip.dto.ResponseDTO;
import com.itrip.pojo.ItripUser;
import com.itrip.service.UserService;
import com.itrip.utils.GenerateCheckCode;
import com.itrip.utils.sendmessage.SendMessageByPhone;
import com.itrip.utils.sendmessage.maisend.MailSender;
import com.itrip.utils.sendmessage.maisend.RegisterMail;
import com.itrip.vo.RegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = {"用户信息接口"})
public class UserController {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Resource
    private UserService userService;

    @ApiOperation("发送验证码")
    @PostMapping("/getCheckCode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageWay",value = "以什么形式发送信息",dataType = "String")
    })
    public ResponseDTO getCheckCode(RegisterVo registerVo, @RequestParam String messageWay) throws Exception {
        //生成验证码
        String checkCode = GenerateCheckCode.getCheckCode();
        //对这个手机验证还是邮件验证进行判断
        if("phone".equals(messageWay)){
           //发送短信
            new SendMessageByPhone().sendMessage(registerVo.getMessage(),checkCode);
        }else if("mail".equals(messageWay)){
            //发送邮件
            RegisterMail.sendMail(checkCode,registerVo.getMessage());
        }
        //将验证码存入redis数据库
        redisTemplate.opsForValue().set(registerVo.getMessage(),checkCode, 6,TimeUnit.HOURS);
        return new ResponseDTO(100,"获取验证码成功",checkCode);
    }


    @ApiOperation("用户注册")
    @PostMapping("/register")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageWay",value = "以什么形式发送信息",dataType = "String")
    })
    public ResponseDTO register(RegisterVo registerVo, @RequestParam String messageWay){
        //获取返还的手机号或邮箱
        String message = registerVo.getMessage();
        Object o = redisTemplate.opsForValue().get(message);
        if(o==null){
            return new ResponseDTO(101,"手机号错误或验证码已失效",null);
        }

        //删除这个验证码
         redisTemplate.delete(registerVo.getMessage());
        //创建一个用户并把它存入数据库
        ItripUser itripUser=new ItripUser();
        itripUser.setUserCode(message);
        itripUser.setUserName(message);
        itripUser.setUserPassword(registerVo.getPassword());
        itripUser.setUserType(0);
        Date date = new Date();
        itripUser.setCreationDate(date);
        System.out.println(itripUser.getCreationDate());
        int count = userService.addItripUser(itripUser);
        return new ResponseDTO(100,"注册成功",null);
    }





}
